
/*
 *  Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.modal.report;

import com.google.common.collect.Lists;
import com.intelligent.ispc.common.utils.StringUtils;
import com.intelligent.ispc.core.dto.YieldDto;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * Created by Lucien.Chen on 2016/8/29.
 */
public class YieldMainDataModel extends AbstractTableModel {
    private String[] columns;
    private List<YieldDto> data;

    /**
     * construction
     *
     * @param columns all columns name
     */
    public YieldMainDataModel(String[] columns) {
        this.columns = columns;
    }

    @Override
    public int getRowCount() {
        if (data == null) {
            return 0;
        } else {
            return data.size();
        }
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        YieldDto yieldDto = data.get(rowIndex);

        switch (columnIndex) {
            case 0:
                String groupKey = StringUtils.replace(yieldDto.getGroupKey(), "@#!", "-");
                return isBlank(groupKey);
            case 1:
                return "<html><u >" + yieldDto.getFirstInspect() + "</u></html>";
            case 2:
                return "<html><u >" + yieldDto.getFirstPass() + "</u></html>";
            case 3:
                return "<html><u >" + yieldDto.getFirstFail() + "</u></html>";
            case 4:
                return isBlank(yieldDto.getFpy()) + "%";
            case 5:
                return "<html><u >" + yieldDto.getReTestInspect() + "</u></html>";
            case 6:
                return "<html><u >" + yieldDto.getReTestPass() + "</u></html>";
            case 7:
                return "<html><u >" + yieldDto.getReTestFail() + "</u></html>";
            case 8:
                return isBlank(yieldDto.getReTestYield()) + "%";
            case 9:
                return isBlank(yieldDto.getTotalYield()) + "%";
            default:
                return "";
        }
    }

    /**
     *
     * @param data new table data
     */
    public void setData(List<YieldDto> data) {
        if (data == null) {
            data = Lists.newArrayList();
        }
        this.data = data;
        fireTableDataChanged();
    }

    public List<YieldDto> getData() {
        return this.data;
    }

    @Override
    public Class<?> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    private String isBlank(String value) {
        if (!StringUtils.isBlank(value)) {
            return value;
        } else {
            return "-";
        }
    }

    private Object isNumeric(String value) {
        if (StringUtils.isNumeric(value)) {
            return value;
        } else {
            return "-";
        }
    }

}
